class Hd {
  //定义状态
  static PENDING = "pending";
  static FULFILLED = "fulfilled";
  static REJECTED = "rejected";
  constructor(executor) {
    this.status = Hd.PENDING;
    this.value = null;
    this.callbacks = [];
    //执行者捕获异常
    try {
      executor(this.resolve.bind(this), this.reject.bind(this));
    } catch (error) {
      this.reject(error);
    }
  }
  resolve(value) {
    //状态保护
    if (this.status == Hd.PENDING) {
      this.status = Hd.FULFILLED;
      this.value = value;
      //pending状态的异步任务处理
      setTimeout(() => {
        this.callbacks.map((callback) => {
          callback.onFulfilled(value);
        });
      });
    }
  }
  reject(reason) {
    if (this.status == Hd.PENDING) {
      this.status = Hd.REJECTED;
      this.value = reason;
      setTimeout(() => {
        this.callbacks.map((callback) => {
          callback.onRejected(reason);
        });
      });
    }
  }
  then(onFulfilled, onRejected) {
    //当onFulfilled, onRejected为null时，再定义成函数
    if (typeof onFulfilled != "function") {
      //实现then的穿透传递
      onFulfilled = () => this.value;
    }
    if (typeof onRejected != "function") {
      onRejected = () => this.value;
    }
    //then返回一个new Promise实现链式操作
    let promise = new Hd((resolve, reject) => {
      //解决同步执行者有异步时pending状态
      if (this.status == Hd.PENDING) {
        this.callbacks.push({
          onFulfilled: (value) => {
            //解决pending状态异常处理
            this.prase(promise, onFulfilled(value), resolve, reject);
          },
          onRejected: (value) => {
            this.prase(promise, onRejected(value), resolve, reject);
          }
        });
      }

      //判断状态
      if (this.status == Hd.FULFILLED) {
        //使用setTimeout使then变成异步
        setTimeout(() => {
          //then链式操作有返回值result，并且是resolve状态
          this.prase(promise, onFulfilled(this.value), resolve, reject);
        });
      }
      if (this.status == Hd.REJECTED) {
        setTimeout(() => {
          this.prase(promise, onRejected(this.value), resolve, reject);
        });
      }
    });
    return promise;
  }
  //复用代码
  prase(promise, result, resolve, reject) {
    if (promise == result) {
      throw new TypeError("Chaining cycle detected");
    }
    try {
      if (result instanceof Hd) {
        result.then(resolve, reject);
      } else {
        resolve(result);
      }
    } catch (error) {
      reject(error);
    }
  }
  static resolve(value) {
    return new Hd((resolve, reject) => {
      if (value instanceof Hd) {
        value.then(resolve, reject);
      } else {
        resolve(value);
      }
    });
  }
  static reject(value) {
    return new Hd((resolve, reject) => {
      if (value instanceof Hd) {
        value.then(resolve, reject);
      } else {
        reject(value);
      }
    });
  }
  static all(promises) {
    //保存每个promise状态
    const values = [];
    return new Hd((resolve, reject) => {
      promises.forEach((promise) => {
        promise.then(
          (value) => {
            values.push(value);
            //当每个promise状态都为resolve，才执行
            if (values.length == promises.length) {
              resolve(values);
            }
          },
          (reason) => {
            reject(reason);
          }
        );
      });
    });
  }
  static race(promises) {
    return new Hd((resolve, reject) => {
      promises.map((promise) => {
        //promise状态改变时不再发生变化，也就是谁快用谁
        promise.then(
          (value) => {
            resolve(value);
          },
          (reason) => {
            reject(reason);
          }
        );
      });
    });
  }
}
